// @ts-nocheck
import fetch from "@blueos.network.fetch"
import call from '../mock/call'
import { getDeviceId } from './deviceId';

const baseUrl = `http://121.37.46.189:8000`
const SUCCESS_CODE = '200'


export const createMeau = (contentText, heart_rate, spo2, calories, stress, category) => {
  const deviceId = getDeviceId();
  return new Promise((resolve, reject) => {
    // if (process.env.NODE_ENV == 'development' && true) {
    //   setTimeout(() => {
    //     resolve(call)
    //   }, 3000)
    //   return
    // }
    fetch.fetch({
      url: `${baseUrl}/v1/call`,
      method: 'post',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 60,
      data: {
        spo2: spo2,
        calories: calories,
        stress: stress,
        sleep_duration: "6.5",
        temperature: "28",
        custom: contentText,
        device_id: deviceId,
        category
      },
      success: (res) => {
        if (res.code == SUCCESS_CODE) {
          resolve(res.data.data)
        } else {
          reject()
        }
      },
      fail: (data, code) => {
        reject()
      }
    })
  })
}


export const setUserInfo = ({ height, weight, age, gender }) => {
  const deviceId = getDeviceId();
  const genderMap = {
    1: '男',
    2: '女'
  }
  fetch.fetch({
    url: `${baseUrl}/v1/setUserInfo`,
    method: 'post',
    responseType: 'json',
    header: { "Content-Type": "application/json" },
    timeout: 60,
    data: { device_id: deviceId, height, weight, age, gender: genderMap[gender] ? genderMap[gender] : '0' },
    success: (res) => {
      if (res.code == SUCCESS_CODE) {
        resolve(res.data.data)
      } else {
        reject()
      }
    },
    fail: (data, code) => {
      reject()
    }
  })
}

export const getCategories = () => {
  return new Promise((resolve, reject) => {
    fetch.fetch({
      url: `${baseUrl}/v1/getCategories`,
      method: 'get',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 60,
      data: {},
      success: (res) => {
        if (res.code == SUCCESS_CODE) {
          resolve(res.data.data)
        } else {
          reject()
        }
      },
      fail: (data, code) => {
        // todo: 请求出错
      }
    })
  })
}

export const getFoodDetails = (foodName) => {
  return new Promise((resolve, reject) => {
    fetch.fetch({
      url: `${baseUrl}/v1/getFoodDetails?food_name=${encodeURIComponent(foodName)}`,
      method: 'get',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 60,
      data: {},
      success: (res) => {
        if (res.code == SUCCESS_CODE) {
          resolve(res.data.data)
        } else {
          reject()
        }
      },
      fail: (data, code) => {
        reject()
      }
    })
  })
}

export const setFeedback = (type, foodName, foodDesc, foodLevel) => {
  const deviceId = getDeviceId();
  return new Promise((resolve, reject) => {
    fetch.fetch({
      url: `${baseUrl}/v1/setFeedback`,
      method: 'post',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 60,
      data: {
        type,
        device_id: deviceId,
        food_name: foodName,
        food_desc: foodDesc,
        food_level: foodLevel
      },
      success: (res) => {
        if (res.code == SUCCESS_CODE) {
          resolve(res.data.data)
        } else {
          reject()
        }
      },
      fail: (data, code) => {
        reject()
      }
    })
  })
}



export const getUserInfo = () => {
  const deviceId = getDeviceId();
  return new Promise((resolve, reject) => {
    fetch.fetch({
      url: `${baseUrl}/v1/getUserInfo?device_id=${deviceId}`,
      method: 'get',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 60,
      data: {},
      success: (res) => {
        if (res.code == SUCCESS_CODE) {
          resolve(res.data.data)
        } else {
          reject()
        }
      },
      fail: (data, code) => {
        reject()
      }
    })
  })
}


export const getFeedbacklist = (type) => {
  const deviceId = getDeviceId();
  return new Promise((resolve, reject) => {
    fetch.fetch({
      url: `${baseUrl}/v1/feedbackList?device_id=${deviceId}&type=${type}`,
      method: 'get',
      responseType: 'json',
      header: { "Content-Type": "application/json" },
      timeout: 60,
      data: {},
      success: (res) => {
        if (res.code == SUCCESS_CODE) {
          resolve(res.data.data)
        } else {
          reject()
        }
      },
      fail: (data, code) => {
        reject()
      }
    })
  })
}